package Demo2_14;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-02-14
 * Time:19:36
 */
import java.util.Stack;

class MyQueue {
    // 用于入队操作的栈
    private Stack<Integer> inStack;
    // 用于出队操作的栈
    private Stack<Integer> outStack;

    public MyQueue() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    // 入队操作
    public void push(int x) {
        inStack.push(x);
    }

    // 出队操作
    public int pop() {
        // 如果出队栈为空，将入队栈中的元素转移到出队栈
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
        return outStack.pop();
    }

    // 查看队首元素
    public int peek() {
        // 如果出队栈为空，将入队栈中的元素转移到出队栈
        if (outStack.isEmpty()) {
            while (!inStack.isEmpty()) {
                outStack.push(inStack.pop());
            }
        }
        return outStack.peek();
    }

    // 判断队列是否为空
    public boolean empty() {
        return inStack.isEmpty() && outStack.isEmpty();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        System.out.println(queue.peek());  // 输出: 1
        System.out.println(queue.pop());   // 输出: 1
        System.out.println(queue.empty()); // 输出: false
    }
}
